<template>
    <div>
        <a-row :span="24">
            <a-col :span="18">
                <a-input-search v-model:value="searchKey" placeholder="请输入部门信息" style="width: 200px; margin-bottom: 10px"
                    @search="onSearch()" />
            </a-col>
            <a-col :span="4">
                <a-dropdown :trigger="['click']">
                    <a class="ant-dropdown-link" :style="{ fontSize: '20px', color: 'black' }" @click.prevent>
                        <DashOutlined />
                    </a>
                    <template #overlay>
                        <a-menu>
                            <a-menu-item><a @click="expandAll">展开全部</a></a-menu-item>
                            <a-menu-item><a @click="collapseAll">折叠全部</a></a-menu-item>
                        </a-menu>
                    </template>
                </a-dropdown>
            </a-col>
        </a-row>

        <a-tree v-model:expandedKeys="expandedKeys" v-model:selectedKeys="selectedKeys" :tree-data="treeData" show-icon
            default-expand-all @select="openDash">
            <template #title="{ deptName }">
                <BankOutlined />
                {{ deptName }}
            </template>
        </a-tree>
    </div>
</template>
<script setup>
import { BankOutlined, DashOutlined } from '@ant-design/icons-vue';
import { queryTree } from "../api/department"
const deptName = ref('')
const treeData = ref([])
const expandedKeys = ref([])
const props = defineProps({
    value: {
        type: [String, Number]
    }
})
const emit = defineEmits(['confirm'])
// 展开全部
function expandAll() {
    const allKeys = getAllNodeKeys(treeData.value)
    expandedKeys.value = allKeys
    console.log('expandedKeys', expandedKeys.value);
}
// 折叠全部
function collapseAll() {
    expandedKeys.value = []
}
// 辅助函数：获取所有节点的键
function getAllNodeKeys(tree) {
    const keys = []
    tree.forEach(node => {
        keys.push(node.id) // 假设节点的唯一标识为 id
        if (node.children && node.children.length > 0) {
            keys.push(...getAllNodeKeys(node.children))
        }
    })
    return keys
}
// 获取Tree
function buildWellTree() {
    queryTree().then(res => {
        //  console.log(res)
        treeData.value = res
    })
}
buildWellTree()

function openDash(key, e) {
    // console.log(e);
    // emit('confirm', e.node.id)
    emit('confirm',key[0])
}

function onSearch() {

}
</script>